<?php
class FeedsController extends AppController {

	var $name = 'Feeds';

	function index() {
		$feeds = $this->Feed->findAll();
		if(isset($this->params['requested'])) {
			return $feeds;
		}
	}

	function admin_index() {
		$this->Feed->recursive = 0;
		$this->set('feeds', $this->paginate());
	}

	function admin_view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Feed.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('feed', $this->Feed->read(null, $id));
	}

	function admin_add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Feed->create();
			if($this->Feed->save($this->data)) {
				$this->Session->setFlash('The Feed has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Feed could not be saved. Please, try again.');
			}
		}
	}

	function admin_edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Feed');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Feed->save($this->data)) {
				$this->Session->setFlash('The Feed saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Feed could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Feed->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Feed');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Feed->del($id)) {
			$this->Session->setFlash('Feed #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}

}
?>